﻿using System;
using System.Collections.Generic;
using System.Text;

using Pixysoft.Framework.XSchEmA.Aspect;
using Pixysoft.Framework.XSchEmA.Entity;

namespace Pixysoft.Framework.XSchEmA.Sql
{
    internal class OracleSqlBuilder : ASqlBuilder
    {
        private OracleSqlBuilder() { }
        private static OracleSqlBuilder instance;
        public static OracleSqlBuilder Instance
        {
            get
            {
                if (instance == null)
                    instance = new OracleSqlBuilder();
                return instance;
            }
        }

        public override string CreateDatabase(string databasename)
        {
            //有待验证 先放着
            return base.CreateDatabase(databasename);
        }
        public override string DropDatabase(string databasename)
        {
            return base.DropDatabase(databasename);
        }

        public override string AlterTableModifyColumn(ColumnSchema column)
        {
            if (column == null)
                return null;
            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + column.TableName + " MODIFY \r\n");
            builder.Append(" " + column.ColumnName + "\t");
            builder.Append(GetDataType(column.DataType));
            builder.Append(CreateDefaultValue(column));
            //builder.Append(";");
            return builder.ToString();
        }
        public override string AlterTableModifyColumnNullable(ColumnSchema column)
        {
            if (column == null)
                return null;
            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + column.TableName + " MODIFY \r\n");
            builder.Append(" " + column.ColumnName + "\t");
            builder.Append(CreateStrongNullable(column));
            //builder.Append(";");
            return builder.ToString();
        }

        public string RenameTable(string oldName, string newName)
        {
            return "ALTER TABLE " + oldName + " RENAME TO " + newName;
        }
        public string RenameColumn(ColumnSchema column, string newName)
        {
            return "ALTER TABLE " + column.TableName + " RENAME " + column.ColumnName + " TO " + newName;

        }


        public string CreateTableComment(TableSchema table)
        {
            if (table == null)
                return null;

            if (table.Comment != null && table.Comment.Trim() != "")
                //return "COMMENT ON TABLE " + table.TableName + " IS '" + table.Comment + "';";
                return "COMMENT ON TABLE " + table.TableName + " IS '" + table.Comment + "'";

            return null;
        }
        public string CreateColumnComment(TableSchema table, ColumnSchema column)
        {
            if (column == null)
                return null;

            if (column.Comment != null && column.Comment.Trim() != "")
                //return "COMMENT ON COLUMN " + table.TableName + "." + column.ColumnName + " IS '" + column.Comment + "';";
                return "COMMENT ON COLUMN " + table.TableName + "." + column.ColumnName + " IS '" + column.Comment + "'";

            return null;
        }

        public string AlterTableDisableConstraint(PrimaryKeySchema pk)
        {
            if (pk == null)
                return null;

            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + pk.TableName + " DISABLE ");
            //builder.Append(" CONSTRAINT " + pk.PkName + ";");
            builder.Append(" CONSTRAINT " + pk.ConsName);
            return builder.ToString();
        }
        public string AlterTableEnableConstraint(PrimaryKeySchema pk)
        {
            if (pk == null)
                return null;

            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + pk.TableName + " ENABLE ");
            //builder.Append(" CONSTRAINT " + pk.PkName + ";");
            builder.Append(" CONSTRAINT " + pk.ConsName);
            return builder.ToString();
        }

        public string AlterTableDisableConstraint(ForeignKeySchema fk)
        {
            if (fk == null)
                return null;
            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + fk.FkTableName + " DISABLE ");
            //builder.Append(" CONSTRAINT " + fk.FkColumnName + ";");
            builder.Append(" CONSTRAINT " + fk.FkColumnNames);
            return builder.ToString();
        }
        public string AlterTableEnableConstraint(ForeignKeySchema fk)
        {
            if (fk == null)
                return null;
            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + fk.FkTableName + " ENABLE ");
            //builder.Append(" CONSTRAINT " + fk.FkColumnName + ";");
            builder.Append(" CONSTRAINT " + fk.FkColumnNames);
            return builder.ToString();
        }

        public string AlterTableDisableConstraint(UniqueSchema unique)
        {
            if (unique == null)
                return null;
            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + unique.TableName + " DISABLE ");
            //builder.Append(" CONSTRAINT " + unique.IndexName + ";");
            builder.Append(" CONSTRAINT " + unique.ConsName);
            return builder.ToString();

        }
        public string AlterTableEnableConstraint(UniqueSchema unique)
        {
            if (unique == null)
                return null;
            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + unique.TableName + " ENABLE ");
            //builder.Append(" CONSTRAINT " + unique.IndexName + ";");
            builder.Append(" CONSTRAINT " + unique.ConsName);
            return builder.ToString();

        }

        public override string AlterTableDropIndex(IndexSchema index)
        {
            if (index == null)
                return null;

            //DROP INDEX {0} ON {1}

            StringBuilder builder = new StringBuilder();

            builder.Append(string.Format("DROP INDEX {0} ON {1}",
                index.ConsName, index.TableName));

            return builder.ToString();

        }
    }
}
